home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / DynamicArrayObjects / CBig2D.c next >
Text File  |  1990-07-01  |  4KB  |  156 lines

  1. /******************************************************************************
  2.  
  3.  CBig2D.c
  4.  
  5. ******************************************************************************/
  6.  
  7. #include "::ErksObjects:CBig2D.h"
  8.  
  9. /*---------------------------------------------------------------------------*/
  10.  
  11. void    CBig2D::IBig2D(void)
  12. {
  13.     /*-- Set Array mapping data to all zeros --*/
  14.     amf.nXLimit = amf.nYLimit = amf.nElementSize = 0;
  15. }
  16.  
  17. /*---------------------------------------------------------------------------*/
  18.  
  19. Boolean    CBig2D::CreateData(int nXMax,int nYMax,int nElementSizeIn,Handle hDataIn)
  20. {
  21.     amf.nXLimit = nXMax; amf.nYLimit = nYMax;
  22.     amf.nElementSize = nElementSizeIn;
  23.     
  24.     if (hDataIn == NULL) {
  25.         gApplication->RequestMemory(FALSE, TRUE);
  26.         hData = NewHandleClear((long) amf.nElementSize * (long) amf.nXLimit * (long) amf.nYLimit);
  27.         gApplication->RequestMemory(FALSE, TRUE);
  28.     }
  29.     else
  30.         hData = hDataIn;
  31.         
  32.     if (hData == NULL) return FALSE;    /* Erk: Display Error Message !!! */
  33.     
  34.     return TRUE;
  35. }
  36.  
  37. /*---------------------------------------------------------------------------*/
  38.  
  39. void    CBig2D::Dispose(void) /* OVERRIDE */
  40. {
  41.     if (hData != NULL) DisposHandle(hData);
  42.     inherited::Dispose();
  43. }
  44.  
  45. /*---------------------------------------------------------------------------*/
  46.  
  47. void    CBig2D::GetValue(int nX,int nY,char *pDestination)
  48. {
  49.     register char    *pData;
  50.     register int    i;
  51.     
  52.     HLock(hData);
  53.     pData= (char *) *hData;
  54.     pData += Offset(nX,nY); 
  55.     
  56.     for (i=0; i < amf.nElementSize; ++i)
  57.       *pDestination++ = *pData++;
  58.     HUnlock(hData);
  59.     return;
  60. }
  61.  
  62. /*---------------------------------------------------------------------------*/
  63.  
  64. void    CBig2D::SetValue(int nX,int nY,char    *pSource)
  65. {
  66.     register char    *pData;
  67.     register int    i;
  68.     
  69.     HLock(hData);
  70.     
  71.     pData= (char *) *hData;
  72.     pData += Offset(nX,nY);
  73.     
  74.     for (i=0; i < amf.nElementSize; ++i) 
  75.        *pData++ = *pSource++;
  76.     HUnlock(hData);
  77.     return;
  78. }
  79.  
  80. /*---------------------------------------------------------------------------*/
  81.  
  82. OSErr    CBig2D::SaveData(CDataFile *datafile)
  83. {
  84.     AMFData2D    amfLocal;    /* Used to avoid implicit dereferencing errors    */
  85.     OSErr        nError;
  86.     long        lDataSize;
  87.     
  88.     /*-- Write array mapping data --*/
  89.     amfLocal = amf;
  90.     nError = datafile->WriteSome((char *) &amfLocal,(long) sizeof(AMFData2D));
  91.     if (nError != NoErr) return nError;
  92.     
  93.     /*-- Write array --*/
  94.     lDataSize = (long) GetHandleSize(hData);
  95.     HLock(hData);
  96.     nError = datafile->WriteSome((char *) *hData, lDataSize);
  97.     HUnlock(hData);
  98.     return nError;
  99. }
  100.  
  101. /*---------------------------------------------------------------------------*/
  102.  
  103. OSErr     CBig2D::LoadData(CDataFile *datafile)
  104. {
  105.     AMFData2D    amfLocal;    /* Used to avoid implicit dereferencing errors    */
  106.     OSErr        nError;
  107.     long        nHandleSize;
  108.     
  109.     /*-- Read array mapping data --*/
  110.     nError = datafile->ReadSome((char *) &amfLocal, (long) sizeof(AMFData2D));
  111.     if (nError != NoErr) return nError;
  112.     amf = amfLocal;
  113.     
  114.     /*-- Allocate Array --*/
  115.     nHandleSize = (long) amf.nElementSize * (long) amf.nXLimit * (long) amf.nYLimit;
  116.     
  117.     gApplication->RequestMemory(FALSE, TRUE);
  118.     hData = NewHandle(nHandleSize);
  119.     gApplication->RequestMemory(FALSE, FALSE);
  120.     if (hData == NULL) return MemError();
  121.     
  122.     /*-- Read array --*/
  123.     HLock(hData);
  124.     nError = datafile->ReadSome((char *) *hData,nHandleSize);
  125.     HUnlock(hData);
  126.     
  127.     return nError;
  128. }
  129.  
  130. /*---------------------------------------------------------------------------*/
  131.  
  132. void    CBig2D::GetDimensions(int *nX, int *nY,int *nElementSizeOut)
  133. {
  134.      *nX = amf.nXLimit;
  135.      *nY = amf.nYLimit;
  136.      *nElementSizeOut = amf.nElementSize;
  137. }
  138.  
  139. /*---------------------------------------------------------------------------*/
  140.  
  141. Handle    CBig2D::GetData(void)
  142. {
  143.     return hData;
  144. }
  145.  
  146. /*---------------------------------------------------------------------------*/
  147.  
  148. long    CBig2D::Offset(int nX,int nY)
  149. {
  150.     return ((long) nY * (long) amf.nXLimit + (long) nX) * (long) amf.nElementSize; 
  151. }
  152.  
  153. /*---------------------------------------------------------------------------*/
  154.  
  155.  
  156.